package com.zzwtec.validation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import com.zzwtec.validation.annotation.Less;

/**
 * @author  草原狼
 *
 * 2017年8月23日
 */

public class LessValidator implements AnnotationValidator {

	@Override
	public ValidateResult validate(Field f, Object value) {
		ValidateResult result = new ValidateResult();
		if(f.isAnnotationPresent(Less.class)){
			Less less = f.getAnnotation(Less.class);
			String type = f.getType().getName();
			if("byte".equals(type) || "java.lang.Byte".equals(type)) {
				if(value == null || (Byte)value >= less.value()){
					result.setMessage(less.fieldName() + "不能超过"+less.value());
				}
			}else if("short".equals(type) || "java.lang.Short".equals(type)) {
				if(value == null || (Short)value >= less.value()){
					result.setMessage(less.fieldName() + "不能超过"+less.value());
				}
			}else if("int".equals(type) || "java.lang.Integer".equals(type)) {
				if(value == null || (Integer)value >= less.value()){
					result.setMessage(less.fieldName() + "不能超过"+less.value());
				}
			}else if("long".equals(type) || "java.lang.Long".equals(type)) {
				if(value == null || (Long)value >= less.value()){
					result.setMessage(less.fieldName() + "不能超过"+less.value());
				}
			}else if("float".equals(type) || "java.lang.Float".equals(type)) {
				if(value == null || (Float)value >= less.value()){
					result.setMessage(less.fieldName() + "不能超过"+less.value());
				}
			}else if("double".equals(type) || "java.lang.Double".equals(type)) {
				if(value == null || (Double)value >= less.value()){
					result.setMessage(less.fieldName() + "不能超过"+less.value());
				}
			}			
		}
		return result;
	}

	private String name;
	
	@Override
	public String getName() {		
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;		
	}

	@Override
	public Class<? extends Annotation> getAnnotationClass() {		
		return Less.class;
	}

}
